package adv

import (
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/validation"
	"qycms/controllers"
	"qycms/models/adv"
	"qycms/untils"
	"strings"
)

type AdvController struct {
	controllers.BaseController
}
//广告列表主页
func(this *AdvController) Index(){
	this.TplName = "adv/index.html"
}

//广告列表数据
func(this *AdvController) AdvList(){
	//分页计算
	currentPage,err := this.GetInt("page",1)
	pagePerNum,_ := this.GetInt("limit",20)
	if(err != nil ){
		currentPage = 1
	}
	offsetNum := pagePerNum * (currentPage - 1)

	//数据查询
	var count int64 = 0
	adv := []adv.Adv{}

	O := orm.NewOrm()
	q := O.QueryTable("sys_adv")

	keyword := this.GetString("key")
	if keyword != ""{
		q = q.Filter("title__icontains",keyword)
	}

	count,_ = q.Filter("is_delete",0).Count()
	q.Filter("is_delete",0).Limit(pagePerNum).Offset(offsetNum).OrderBy("-create_time").RelatedSel().All(&adv)

	this.Data["json"] = untils.ListJson(count,adv)
	this.ServeJSON()
	this.StopRun()
}

//新建广告页面
func(this *AdvController) AddAdvView(){
	o := orm.NewOrm()
	place := []adv.AdvType{}
	o.QueryTable("sys_adv_type").Filter("is_active",1).Filter("is_delete",0).All(&place)
	this.Data["place"] = place
	this.TplName = "adv/add.html"
}

//新建广告
func(this *AdvController) AddAdv(){
	//接收参数
	title := this.GetString("title")
	shortitle := this.GetString("shortitle")
	url := this.GetString("url")
	cate,_ := this.GetInt("cate")
	is_active,_ := this.GetInt("is_active")
	mark := this.GetString("mark")
	sort,_ := this.GetInt("sort")

	fileurl := this.GetString("pic")
    pic := this.Base64Upload(fileurl)

	//数据验证
	valid := validation.Validation{}
	valid.Required(title,"title").Message("广告标题不能为空")
	valid.Required(shortitle,"shortitle").Message("广告副标题不能为空")
	valid.Required(url,"url").Message("跳转地址不能为空")
	valid.Required(cate,"cate").Message("广告位不能为空")
	valid.Required(is_active,"is_active").Message("状态不能为空")
	valid.Required(mark,"mark").Message("备注不能为空")


	//绑定到模型
	types := adv.AdvType{}

	o := orm.NewOrm()
	o.QueryTable(new(adv.AdvType)).Filter("id",cate).One(&types)
	adv := adv.Adv{
		Title: title,
		Shortitle: shortitle,
		Pics:pic,
		Url: url,
		Mark: mark,
		Sort: sort,
		IsActive: is_active,
		AdvType:&types,
	}

	_ ,err := o.Insert(&adv)

	if err != nil {
		this.Err(2001,"操作失败",nil)
	}else{
		this.Suc(nil)
	}
}

//更新广告页面
func(this *AdvController) UpdateAdvView(){
	id,_ := this.GetInt("id")
	o := orm.NewOrm()

	place := []adv.AdvType{}
	o.QueryTable("sys_adv_type").Filter("is_active",1).Filter("is_delete",0).All(&place)

	adv := adv.Adv{}
	o.QueryTable("sys_adv").Filter("id",id).Filter("is_delete",0).One(&adv)

	this.Data["place"] = place
	this.Data["adv"] = adv
	this.TplName = "adv/edit.html"
}

//更新广告
func(this *AdvController) UpdateAdv(){
	//接收参数
	ID := this.GetString("Id")
	title := this.GetString("title")
	shortitle := this.GetString("shortitle")
	url := this.GetString("url")
	cate,_ := this.GetInt("cate")
	is_active:= this.GetString("is_active")
	mark := this.GetString("mark")
	sort,_ := this.GetInt("sort")

	fileurl := this.GetString("pic")
	var pic string
	if len(fileurl) == 0 {
		pic = this.GetString("oldpic")
	}else{
		pic = this.Base64Upload(fileurl)
	}

	//数据验证
	valid := validation.Validation{}
	valid.Required(title,"title").Message("广告标题不能为空")
	valid.Required(shortitle,"shortitle").Message("广告副标题不能为空")
	valid.Required(url,"url").Message("跳转地址不能为空")
	valid.Required(cate,"cate").Message("广告位不能为空")
	valid.Required(is_active,"is_active").Message("状态不能为空")
	valid.Required(mark,"mark").Message("备注不能为空")


	o := orm.NewOrm()

	//绑定到模型
	var new_is_active int
	if is_active == "on"{
		new_is_active = 1
	}else{
		new_is_active = 0
	}

	adv := orm.Params{
		"title": title,
		"shortitle": shortitle,
		"url": url,
		"adv_type_id": cate,
		"mark": mark,
		"sort": sort,
		"pics":pic,
		"isActive": new_is_active,
	}

	_ ,err := o.QueryTable("sys_adv").Filter("id",ID).Update(adv)

	if err != nil {
		this.Err(2001,"操作失败",nil)
	}else{
		this.Suc(nil)
	}
}

//删除广告
func(this *AdvController) DelAdv(){
	//接收参数
	ids := this.GetString("ids");
	new_ids := ids[:len(ids)]
	id_arr := strings.Split(new_ids,",")

	//数据更新
	o := orm.NewOrm()
	qs := o.QueryTable("sys_adv")
	for _,v :=range id_arr{
		id_int := untils.StrToInt(v)
		qs.Filter("id",id_int).Update(orm.Params{
			"is_delete":1,
		})
	}
	this.Suc(nil)
}


//广告位主页面
func (this *AdvController) Cate()  {
	this.TplName = "adv/cate.html"
}

//广告位主页数据
func (this *AdvController) CateList()  {
	//分页计算
	currentPage,err := this.GetInt("page",1)
	pagePerNum,_ := this.GetInt("limit",20)
	if(err != nil ){
		currentPage = 1
	}
	offsetNum := pagePerNum * (currentPage - 1)

	//数据查询
	var count int64 = 0
	cate := []adv.AdvType{}

	O := orm.NewOrm()
	q := O.QueryTable("sys_adv_type")

	keyword := this.GetString("key")
	if keyword != ""{
		q = q.Filter("tname__icontains",keyword)
	}

	count,_ = q.Filter("is_delete",0).Count()
	q.Filter("is_delete",0).Limit(pagePerNum).Offset(offsetNum).All(&cate)

	this.Data["json"] = untils.ListJson(count,cate)
	this.ServeJSON()
	this.StopRun()
}

//新建广告位页面
func (this *AdvController) AddCateView()  {
	this.TplName = "adv/addCate.html"
}

//新建广告位
func (this *AdvController) AddCate()  {
	//接收参数
	tname := this.GetString("tname")
	sort,_ := this.GetInt("sort")
	mark := this.GetString("mark")
	is_active,_ := this.GetInt("is_active")

	//数据验证
	valid := validation.Validation{}
	valid.Required(tname,"tname").Message("广告位名称不能为空")
	valid.Required(is_active,"is_active").Message("开启状态不能为空")
	valid.Required(mark,"mark").Message("备注不能为空")

	o := orm.NewOrm()
	//用户账号唯一性验证
	iscate := o.QueryTable("sys_adv_type").Filter("tname",tname).Filter("is_delete",0).Exist()

	if iscate {
		this.Err(2001,"广告位已经存在",nil)
	}

	//绑定到模型
	cate := adv.AdvType{
	    Tname: tname,
	    Sort: sort,
		Mark: mark,
		IsActive: is_active,
	}

	_ ,err := o.Insert(&cate)

	if err != nil {
		this.Err(2001,"操作失败",nil)
	}else{
		this.Suc(nil)
	}
}

//更新广告位页面
func (this *AdvController) UpdateCateView()  {
	id,_ := this.GetInt("id")
	o := orm.NewOrm()
	cate := adv.AdvType{}
	o.QueryTable("sys_adv_type").Filter("id",id).One(&cate)
	this.Data["cate"] = cate
	this.TplName = "adv/editCate.html"
}

//更新广告位
func (this *AdvController) UpdateCate()  {
	//接收参数
	ID := this.GetString("Id")
	tname := this.GetString("tname")
	is_active := this.GetString("is_active")
	mark := this.GetString("mark")
	sort,_:= this.GetInt("sort")

	//数据验证
	valid := validation.Validation{}
	valid.Required(ID,"Id").Message("请选择修改的数据")
	valid.Required(tname,"tname").Message("广告位不能为空")
	valid.Required(is_active,"is_active").Message("开启状态不能为空")
	valid.Required(mark,"mark").Message("备注不能为空")


	o := orm.NewOrm()
	//用户账号唯一性验证
	iscate := o.QueryTable("sys_adv_type").Exclude("id",ID).Filter("tname",tname).Filter("is_delete",0).Exist()

	if iscate {
		this.Err(2001,"广告位已经存在",nil)
	}

	//绑定到模型
	var new_is_active int
	if is_active == "on"{
		new_is_active = 1
	}else{
		new_is_active = 0
	}

	cate := orm.Params{
		"tname": tname,
		"mark": mark,
		"sort":sort,
		"isActive": new_is_active,
	}

	_ ,err := o.QueryTable("sys_adv_type").Filter("id",ID).Update(cate)

	if err != nil {
		this.Err(2001,"操作失败",nil)
	}else{
		this.Suc(nil)
	}
}

//删除广告位
func (this *AdvController) DelCate()  {
	//接收参数
	ids := this.GetString("ids");
	new_ids := ids[:len(ids)]
	id_arr := strings.Split(new_ids,",")

	//数据更新
	o := orm.NewOrm()
	qs := o.QueryTable("sys_adv_type")
	for _,v :=range id_arr{
		id_int := untils.StrToInt(v)
		qs.Filter("id",id_int).Update(orm.Params{
			"is_delete":1,
		})
	}
	this.Suc(nil)
}

